package com.yanqu.road.dao.impl.admin;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.player.InternalUserIpLocation;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class AdminCommonDaoImpl2 extends TempDao {

    public boolean deleteInternalUserIpLocation(long userId) {
        String sql = "delete from t_s_internal_user where userId=? ";
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userId));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public boolean addInternalUserIpLocation(long userId, int channelId, long serverId, String ipLocation) {
        String sql = "insert into t_s_internal_user(`userId`, `serverId`, `channelId`, `ipLocation`)values(?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userId));
        para.put(i++, new DbParameter(Types.BIGINT, serverId));
        para.put(i++, new DbParameter(Types.INTEGER, channelId));
        para.put(i++, new DbParameter(Types.VARCHAR, ipLocation));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public boolean updateInternalUserIpLocation(long userId, String ipLocation) {
        String sql = "update `t_s_internal_user` set ipLocation=? where userId = ?";
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, ipLocation));
        para.put(i++, new DbParameter(Types.BOOLEAN, userId));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public InternalUserIpLocation getInternalUserIpLocation(long userId) {
        String sql = "select * from t_s_internal_user where userId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Object> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                InternalUserIpLocation data = new InternalUserIpLocation();
                data.setUserId(rs.getLong("userId"));
                data.setIpLocation(rs.getString("ipLocation"));
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public boolean repeatedChannelParams(int channelId, String param) {
        String sql = "select * from t_s_param where channelId = ? and param = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, channelId);
            call.setString(i++, param);
            rs = call.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return false;
    }
}
